#include "translate.h"

void translateToProgGraph(DAGedges &dag, NetworkGraph & pg){
	//first of all I will assign to each node its id. 
	//then to each node that's at the bottom,
	
	for (DAGedges::iterator it = dag.begin(); it != dag.end(); ) {
		DAGnodeID PSI;
		PSI.first = it->first;		
		//visit (PSI, it->second, 1, fout);
		PSI.second = it-dag.begin();
		NetworkGraph::ProgStmt *s = new NetworkGraph::ProgStmt(PSI);
		pg.addNode(s);
		NetworkGraph::ProgExpr *e = new NetworkGraph::ProgExpr(PSI);
		pg.addNode(e);
		int temp = EDGE_WEIGHT; //EDGE_WEIGHT needs to be passed by reference and it's a const variable
		pg.addEdge(s,e,temp);	
		std::string sempty;
		while (it != dag.end() && (it->first)==PSI.first) {
			DAGnodeID temp = make_pair(it->second,it-dag.begin());
			NetworkGraph::ProgLit *l = new NetworkGraph::ProgLit(temp,sempty);
			int temp2=EDGE_WEIGHT;
			pg.addEdge(e,l,temp2);			
			it++;
		}
	}
}
void translateFromProgGraph( NetworkGraph &ng, DAGedges &dag){
	ng.compact();
	dag.clear();
	for(int i=0;i<(int)ng.vertices.size();i++){
		if(ng.vertices.at(i)->type()==(int)NetworkGraph::Statement){
			DAGnode nd = ng.vertices.at(i)->data.first;					
			if(ng.vertices.at(i)->edges.size()>0){
				NetworkGraph::ProgNode *p = &(ng.vertices.at(i)->edges.back()->to);
				if(p->edges.size()>0){
					for(list <NetworkGraph::ProgEdge * >::iterator it=p->edges.begin();it!=p->edges.end();it++){
						DAGnode nd2 = (*it)->to.data.first;						
						dag.push_back(make_pair(nd,nd2));						
					}
				}
			}
		}		
	}
}
